﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
    public partial class wfGestionDescMnt : EditPage
    {
        public const int _GRDLIST_STCUOTA = 11;
        public const int _GRDLIST_STANULADO = 12;

        public Nullable<DateTime> DTFeIniProc
        {
            get {
                if (this.ViewState["_dtfeIniP"] != null)
                    return DateTime.Parse(this.ViewState["_dtfeIniP"].ToString());
                else
                    return null;
            
            }
            set { this.ViewState["_dtfeIniP"] = value; }
        }

        public DataTable DataSourceSaldos
        {
            get { return (DataTable)this.ViewState["_dsSal"]; }
            set { this.ViewState["_dsSal"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                this.LoadInPostBack();
                return;
            }
            this.LoadData();
        }

        public override void LoadInPostBack()
        {
            if (this.DataSourceSaldos != null)
            {
                if (this.DataSourceSaldos.Select("idPersonal='" + hdnidPersonal.Value + "'", "").Length == 0)
                    GetSaldosVacaciones();
            }
            else
                GetSaldosVacaciones();
            this.RegisterGeneralScripts();
        }

        public override void LoadInitialSettings()
        {
            this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
            this.SetInitialTitle("Gestion de Descanso");
        }

        public override void LoadListControls()
        {
            ETablaGenDet objEDet = new ETablaGenDet();
            MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

            objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
            DataTable dt = objBOI.GetDetailByList(objEDet);
            WebHelper.LoadListControl(ddlidForPago, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);

            this.hdnnuDiasTotal.Value = "0";
        }

        public override void DataToControls()
        {

            EPerVacaDesca objE = new EPerVacaDesca();
            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.PerVacaDesca));
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
            objE.IdPersonal = Request.QueryString["IdPersonal"].ToString();
            objE.IdVacaDesca = Request.QueryString["IdVacaDesca"].ToString();
            objE = (EPerVacaDesca)objBO.GetByKey(objE);
            if (objE != null)
            {
                WebHelper.EntityToControl(objE, "IdVacaDesca", txtidVacaDesca);
                WebHelper.EntityToControl(objE, "IdForPago", ddlidForPago);
                WebHelper.EntityToControl(objE, "DsObserva", txtdsObservacion);

                EPersonal EPer = new EPersonal();
                IBOQuery BOPer = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Personal));
                EPer.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                EPer.IdPersonal = Request.QueryString["IdPersonal"].ToString();
                EPer = (EPersonal)BOPer.GetByKey(EPer);
                if (EPer != null)
                {
                    hdnfeIngrPlani.Value = EPer.FeIngpla.ToString();
                    if (EPer.FeIngpla.ToString() != "")
                    { 
                        txtfeIngrPlaniPp.Text = DateTime.Parse(EPer.FeIngpla.ToString()).ToString("dd/MM/yyyy");
                    }

                    ECliProv ECli = new ECliProv();
                    IBOQuery BOCli = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CliProv));
                    ECli.IdCliProv = EPer.IdCliProv;
                    ECli = (ECliProv)BOCli.GetByKey(ECli);
                    if (ECli != null)
                    {
                        txtidPersonal.Text = ECli.DsRazSocial;
                    }

                }

            }

            EPerVacaDescaDet objEDet = new EPerVacaDescaDet();
            RHBOQry.IPerVacaDesca objBODet = (RHBOQry.IPerVacaDesca)WCFHelper.GetObject<RHBOQry.IPerVacaDesca>(typeof(RHBOQry.PerVacaDesca));
            objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEDet.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
            hdnidPersonal.Value = Request.QueryString["IdPersonal"].ToString();
            objEDet.IdPersonal = Request.QueryString["IdPersonal"].ToString();
            objEDet.IdVacaDesca = Request.QueryString["IdVacaDesca"].ToString();
            DataTable dt = objBODet.GetByParentKeyVacaDesca(objEDet);
            this.GridSetDataBinding(grdList, dt);

            GetSaldosVacaciones();

        }

        public override void RegisterGeneralScripts()
        {
  
            this.chkCalcular.Attributes.Add("onclick", "calcularMonto();");
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("var varRet = modalDialog('wfPersonal.aspx',750,400);");
            sb.AppendLine("if(varRet==null)");
            sb.AppendLine("return false;");
            sb.AppendLine("ctl = document.getElementById('" + this.hdnidPersonal.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[0];");
            sb.AppendLine("ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[1];");
            sb.AppendLine("document.getElementById('" + this.hdnfeIngrPlani.ClientID + "').value = varRet[6];");
            sb.AppendLine("__doPostBack('GetSaldosVacaciones', '');");
            this.txtidPersonal.OnClientClick = sb.ToString();

            sb = new StringBuilder();

            sb.AppendLine("var varRet = modalDialog('wfPeriodoPlani.aspx?IdPersonal=' + document.getElementById('" + this.hdnidPersonal.ClientID + "').value + '&IdForPago=' +document.getElementById('" + this.ddlidForPago.ClientID + "').value ,750,400);");
            sb.AppendLine("if(varRet==null)");
            sb.AppendLine("return false;");
            sb.AppendLine("ctl = document.getElementById('" + this.hdnidPlanilla.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[2];");
            sb.AppendLine("ctl = document.getElementById('" + this.txtidPlanillaPp.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[3] + ' - ' + varRet[4] + ' -   [ ' + varRet[7] + ' - ' + varRet[8] + ' ]';");
            sb.AppendLine("ctl = document.getElementById('" + this.hdnnuProcAnual.ClientID + "');");
            sb.AppendLine("ctl.value = varRet[4];");
            sb.AppendLine("document.getElementById('" + this.hdnFeIniProcAnual.ClientID + "').value = varRet[7];");
            sb.AppendLine("document.getElementById('" + this.hdnFeFinProcAnual.ClientID + "').value = varRet[8];");
            sb.AppendLine("$('#" + this.txtfeVacaPp.ClientID + "').datepicker('change',{minDate: varRet[7],maxDate: varRet[8]});");
            this.txtidPlanillaPp.OnClientClick = sb.ToString();

            sb = new StringBuilder();

            sb.AppendLine("function validateEditForm(key){");
            sb.AppendLine("var varRpta = isEmptyControl('" + this.txtidPersonal.ClientID + "','Ingrese el Personal');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidForPago.ClientID + "','Seleccione la forma de pago');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
            sb.AppendLine("}");

            sb.AppendLine("function validateEditFormGrid(key){");
            sb.AppendLine("varRpta = isEmptyControl('" + this.txtidPersonal.ClientID + "','Seleccione el Personal.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidForPago.ClientID + "','Seleccione la Forma de Pago.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("menu_doPostBack('" + mnuGrid.ClientID + "','" + mnuGrid.UniqueID + "',key);");
            sb.AppendLine("}");

            sb.AppendLine("function OnChangeRangeDates(){");
            sb.AppendLine("varRpta = isEmptyControl('" + this.txtidPlanillaPp.ClientID + "','Seleccione la Planilla.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("{return !varRpta;");
            sb.AppendLine("document.getElementById('" + this.hdnFeFinProcAnual.ClientID + "').value = '';document.getElementById('" + this.txtfeVacaPp.ClientID + "').value = '';}");
            sb.AppendLine("dFin = document.getElementById('" + this.hdnFeFinProcAnual.ClientID + "').value;");
            sb.AppendLine("   if(dFin!=''){");
            sb.AppendLine("      var numDias = 1 + DateDiff('d',$('#" + this.txtfeVacaPp.ClientID + "').datepicker('getDate'),getDateFromFormat(dFin,'dd/MM/yyyy'));");
            sb.AppendLine("      document.getElementById('" + this.hdnMaxDiasRangoFe.ClientID + "').value = numDias;");
            //sb.AppendLine("       alert(numDias);");
            sb.AppendLine("   }");
            sb.AppendLine("}");

            ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);

            this.txtfeVacaPp.OnClientClickDay = "OnChangeRangeDates();";
            RegisterEditPopupScripts();
        }

        public void RegisterEditPopupScripts()
        {

            //this.txtidForPagoPp.Attributes.Add("readonly", "true");
            //this.txtnuCuotaPp.Attributes.Add("readonly", "true");
            //this.txtnuProcAnualPp.Attributes.Add("readonly", "true");

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("function validate" + this.bttOkPopup.ID + "(){");
            sb.AppendLine("varRpta = isEmptyControl('" + this.txtidPlanillaPp.ClientID + "','Seleccione la Planilla.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeVacaPp.ClientID + "','Seleccione la Fecha.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            //sb.AppendLine("varRpta = isEmptyControl('" + this.txtPeriVacPp.ClientID + "','Ingrese el Periodo del que se descontaran los dias.');");
            //sb.AppendLine("if(varRpta==true)");
            //sb.AppendLine("return !varRpta;");
            sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuDiasVacaPp.ClientID + "','Ingrese el numero de Dias.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            //sb.AppendLine("varRpta = isEmptyControl('" + this.txtmtMontoPp.ClientID + "','Ingrese el monto.');");
            //sb.AppendLine("if(varRpta==true)");
            //sb.AppendLine("return !varRpta;");

            sb.AppendLine("if( GetSaldoPeriodo() <= 0){");
            sb.AppendLine("   if(!confirm('No contiene suficiente Saldo, desea continuar.'))");
            sb.AppendLine("   {return false;}");
            sb.AppendLine("}");


            sb.AppendLine("if(parseFloat(document.getElementById('" + this.hdnMaxDiasRangoFe.ClientID + "').value) < parseFloat(document.getElementById('" + this.txtnuDiasVacaPp.ClientID + "').value)){");
            sb.AppendLine("alert('El numero de Dias ingresado es mayor al rango de fechas selecionado');return false;}");
            sb.AppendLine("return true;}");

            ScriptManager.RegisterStartupScript(this, this.GetType(), "validatePopup", sb.ToString(), true);

            this.bttOkPopup.OnClientClick = "return validate" + bttOkPopup.ID + "();";
            this.bttCancelPopup.OnClientClick = "$find('" + this.mdlPopup.ClientID + "').hide();return false;";

        }

        protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
        {
            this.bttOkPopup.Focus(); 
            if (e.Item == null)
                return;

            switch (e.Item.Value)
            {
                case ConstMenuToolbarValue.BUTTON_SAVE:
                case ConstMenuToolbarValue.BUTTON_SAVENEW:
                    if (this.FormEditAction != FormEditAction.Delete)
                        this.SaveData(e.Item.Value);
                    else
                        this.DeleteData();
                    break;
                case ConstMenuToolbarValue.BUTTON_EXIT:
                    this.Response.Redirect("wfGestionDescBnd.aspx");
                    break;
            }
        }

        protected void mnuGrid_MenuItemClick(Object sender, MenuEventArgs e)
        {
            if (e.Item == null)
                return;

            switch (e.Item.Value)
            {
                case ConstMenuToolbarValue.BUTTON_NEW:
                    this.GridEditAction = GridEditAction.AddNew;
                    this.lblTitlePopup.Text = "Gestion de descanso(Nuevo)";
                    this.RegisterEditPopupScripts();
                    this.ClearEditPopup();
                    this.txtfeIngrPlaniPp.Text = this.hdnfeIngrPlani.Value;
                    this.txtnuDiasVacaPp.Enabled = true;
                    this.chkCalcular.Checked = true;
                    this.mdlPopup.Show();
                    break;
            }

        }

        protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

            switch (e.CommandName)
            {
                case ConstGridRowCommand.GRIDEDIT:
                    this.bttOkPopup.Focus();
                    this.lblTitlePopup.Text = "Gestion de descanso(Editar)";
                    this.GridEditAction = GridEditAction.Edit;
                    this.mdlPopup.Show();
                    this.LoadDataEditPopup(grdList, this.EditRowIndex);
                    this.RegisterEditPopupScripts();
                    this.txtnuDiasVacaPp.Enabled = false;
                    this.chkCalcular.Checked = true;
                    break;
            }
        }

        protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
        {

        }

        public void GetSaldosVacaciones()
        {
            EPerVacaProv objEProv = new EPerVacaProv();
            IBOQuery objBOProv = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.PerVacaProv));
            objEProv.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEProv.IdPersonal = hdnidPersonal.Value.ToString();
            this.DataSourceSaldos = objBOProv.GetList(objEProv);
            grdListSaldos.DataSource = DataSourceSaldos;
            grdListSaldos.DataBind();

            grdListSaldoPp.DataSource = DataSourceSaldos;
            grdListSaldoPp.DataBind();

        }

        protected void bttOkPopup_Click(object sender, EventArgs e)
        {
            if (DTFeIniProc == null)
            {
                DTFeIniProc = DateTime.Parse(this.hdnFeIniProcAnual.Value);
            }

            string strDsConcepto = "";
            string strDsStVaca = "";
            decimal dcMtoPeriodoActual = 0;
            EConcepRemu objEDet = new EConcepRemu();
            IBOQuery objBODet = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ConcepRemu));
            objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEDet.IdConRemu = ConstConceptoRemu.VACACIONES;
            objEDet = (EConcepRemu)objBODet.GetByKey(objEDet);
            if (objEDet != null)
                strDsConcepto = objEDet.DsConRemu;
            else
                strDsConcepto = "";
                  
            ETablaGenDet objE = new ETablaGenDet();
            MGBOQry.ITablaGen objBO = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
            objE.IdTabla = ConstMultitabla.ESTADO_VACACIONES;
            objE.IdCodigo = ConstEstadoCuotaPresta.PENDIENTE;
            objE = (ETablaGenDet)objBO.GetDetailByKey(objE);
            if (objEDet != null)
                strDsStVaca = objE.DsDescrip;
            else
                strDsStVaca = "";
            
            Object objResNuSec = null;
            int intResNusec = 0;
            DataTable dt = (DataTable)this.DataSource;
            DataRow dr = null;

            if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
            {
                objResNuSec = dt.Compute("MAX(nuSecuen)", "");
                if (objResNuSec == null || objResNuSec.ToString() == "")
                    intResNusec = 0;
                else
                    intResNusec = Convert.ToInt32(objResNuSec);
                intResNusec += 1;
                dr = dt.NewRow();
                dr["nuSecuen"] = intResNusec;
            }
            else
            {
                dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
            }

            if (this.GridEditAction != GridEditAction.Delete)
            {
                decimal deNuDias = decimal.Parse(txtnuDiasVacaPp.Text);
                decimal MontoXDia = 0;


                if (this.chkCalcular.Checked)
                {

                    EPerVacaDescaDet objEMtVac = new EPerVacaDescaDet();
                    RHBOQry.IPerVacaDesca objBOMtVac = (RHBOQry.IPerVacaDesca)WCFHelper.GetObject<RHBOQry.IPerVacaDesca>(typeof(RHBOQry.PerVacaDesca));
                    objEMtVac.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                    objEMtVac.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
                    objEMtVac.IdPersonal = hdnidPersonal.Value;
                    objEMtVac.FeVaca = DTFeIniProc.Value;
                    DataTable dtmtVac = objBOMtVac.GetMontoRemuTotal(objEMtVac);
                    
                    if(dtmtVac.Rows.Count > 0)
                        foreach (DataRow drmtVac in dtmtVac.Rows)
                        {
                            MontoXDia = decimal.Parse(drmtVac["mtRemuTotal"].ToString()) / 30;
                        }
                    else
                        MontoXDia = decimal.Parse(txtmtMontoPp.Text) / decimal.Parse(txtnuDiasVacaPp.Text);
                }
                else
                {
                    MontoXDia = decimal.Parse(txtmtMontoPp.Text) / decimal.Parse(txtnuDiasVacaPp.Text);
                }


                //hdnnuDiasTotal.Value = (decimal.Parse(hdnnuDiasTotal.Value) + deNuDias).ToString();
                DateTime dtDate = DateTime.Parse(txtfeVacaPp.Text);

                foreach (DataRow drSal in this.DataSourceSaldos.Rows)
                {
                    if( dr["idPeriVac"].ToString() == Session[ConstSessionVar.PERIODO].ToString())
                        dcMtoPeriodoActual = decimal.Parse(drSal["mtSaldo"].ToString()) - decimal.Parse(drSal["mtDiasGozado"].ToString());

                    drSal.BeginEdit();
                    if (this.GridEditAction == GridEditAction.Edit)
                        if(dr["idPeriVac"].ToString() == drSal["idPeriVac"].ToString())
                        drSal["mtDiasGozado"] = decimal.Parse(drSal["mtDiasGozado"].ToString()) - decimal.Parse(dr["nuDiasVaca"].ToString());
                           


                    if (int.Parse(drSal["idPeriVac"].ToString()) <= int.Parse(Session[ConstSessionVar.PERIODO].ToString()))
                    {
                        decimal mtsaldo = decimal.Parse(drSal["mtSaldo"].ToString()) - decimal.Parse(drSal["mtDiasGozado"].ToString());

                        if (mtsaldo <= 0)
                            continue;

                        if (this.GridEditAction == GridEditAction.AddNew)
                           dr["nuSecuen"] = intResNusec;

                        dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
                        dr["idPersonal"] = hdnidPersonal.Value;
                        dr["idPeriodo"] = Session[ConstSessionVar.PERIODO].ToString();
                        dr["idVacaDesca"] = txtidVacaDesca.Text;
                        dr["idPeriVac"] = drSal["idPeriVac"].ToString();
                        dr["idPlanilla"] = hdnidPlanilla.Value;
                        dr["dsPlanilla"] = txtidPlanillaPp.Text;
                        dr["idForPago"] = ddlidForPago.SelectedItem.Value;
                        dr["dsForPago"] = ddlidForPago.SelectedItem.Text;
                        dr["nuProcAnual"] = hdnnuProcAnual.Value;
                        dr["idConRemu"] = ConstConceptoRemu.VACACIONES;
                        dr["feDesde"] = DateTime.Parse(hdnFeIniProcAnual.Value);
                        dr["feHasta"] = DateTime.Parse(hdnFeFinProcAnual.Value);
                        dr["idConRemu"] = ConstConceptoRemu.VACACIONES;
                        dr["dsConRemu"] =  strDsConcepto;
                        dr["stVaca"] = ConstEstadoVacaciones.PENDIENTE;
                        dr["dsStVaca"] = strDsStVaca;
                        dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;

                        if (mtsaldo > deNuDias)
                        {
                            dr["nuDiasVaca"] = deNuDias;
                            dr["mtVaca"] = Math.Round(deNuDias * MontoXDia,2);
                            dr["feVaca"] = dtDate.ToString("dd/MM/yyyy");
                            dtDate = dtDate.AddDays((int)deNuDias);
                            drSal["mtDiasGozado"] = decimal.Parse(drSal["mtDiasGozado"].ToString()) + deNuDias;

                            deNuDias = deNuDias - deNuDias;
                            if (this.GridEditAction == GridEditAction.AddNew)
                                dt.Rows.Add(dr);
                            break;
                        }
                        else
                        {
                            dr["nuDiasVaca"] = mtsaldo;
                            dr["mtVaca"] =Math.Round( mtsaldo * MontoXDia,2);
                            dr["feVaca"] = dtDate.ToString("dd/MM/yyyy");
                            dtDate = dtDate.AddDays((int)mtsaldo);
                            drSal["mtDiasGozado"] = decimal.Parse(drSal["mtDiasGozado"].ToString()) + mtsaldo;
                            if (this.GridEditAction == GridEditAction.AddNew)
                                dt.Rows.Add(dr);
                            deNuDias = deNuDias - mtsaldo;
                            dr = dt.NewRow();
                            intResNusec = intResNusec + 1;
                        }
                    }
                    drSal.EndEdit();
                }

                if (deNuDias > 0)
                {
                    dr = dt.NewRow();
                    intResNusec = intResNusec + 1;
                    dr["nuSecuen"] = intResNusec;
                    dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
                    dr["idPersonal"] = hdnidPersonal.Value;
                    dr["idPeriodo"] = Session[ConstSessionVar.PERIODO].ToString();
                    dr["idVacaDesca"] = txtidVacaDesca.Text;
                    if (dcMtoPeriodoActual - deNuDias >= -30)
                        dr["idPeriVac"] = int.Parse(Session[ConstSessionVar.PERIODO].ToString());
                    else
                        dr["idPeriVac"] = int.Parse(Session[ConstSessionVar.PERIODO].ToString()) + 1;
                    dr["idPlanilla"] = hdnidPlanilla.Value;
                    dr["dsPlanilla"] = txtidPlanillaPp.Text;
                    dr["idForPago"] = ddlidForPago.SelectedItem.Value;
                    dr["dsForPago"] = ddlidForPago.SelectedItem.Text;
                    dr["nuProcAnual"] = hdnnuProcAnual.Value;
                    dr["idConRemu"] = ConstConceptoRemu.VACACIONES;
                    dr["feDesde"] = DateTime.Parse(hdnFeIniProcAnual.Value);
                    dr["feHasta"] = DateTime.Parse(hdnFeFinProcAnual.Value);
                    dr["idConRemu"] = ConstConceptoRemu.VACACIONES;
                    dr["dsConRemu"] = strDsConcepto;
                    dr["stVaca"] = ConstEstadoVacaciones.PENDIENTE;
                    dr["dsStVaca"] = strDsStVaca;
                    dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;
                    dr["nuDiasVaca"] = deNuDias;
                    dr["mtVaca"] = Math.Round(deNuDias * MontoXDia,2);
                    dr["feVaca"] = dtDate.ToString("dd/MM/yyyy");
                    if (this.GridEditAction == GridEditAction.AddNew)
                        dt.Rows.Add(dr);
                }


            }
            else
            {
                dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
            }

            this.GridSetDataBinding(grdList, dt);
        }

        public override void LoadDataEditPopup(GridView grid, int rowIndex)
        {
            this.ClearEditPopup();

            DataRow dr = this.GridGetRowFromDataKey(this.DataSource, grdList, rowIndex);

            if (dr == null)
                return;

            WebHelper.RowToControl(dr, "idPlanilla", this.hdnidPlanilla);
            WebHelper.RowToControl(dr, "dsPlanilla", this.txtidPlanillaPp);
            WebHelper.RowToControl(dr, "dsPlanilla", this.txtidPlanillaPp);
            WebHelper.RowToControl(dr, "nuDiasVaca", this.txtnuDiasVacaPp);
            WebHelper.RowToControl(dr, "nuProcAnual", this.hdnnuProcAnual);
            WebHelper.RowToControl(dr, "feVaca", this.txtfeVacaPp);
            WebHelper.RowToControl(dr, "mtVaca", this.txtmtMontoPp);

            txtfeVacaPp.DateMin = DateTime.Parse(dr["feDesde"].ToString()).AddDays(-1);
            txtfeVacaPp.DateMax = DateTime.Parse(dr["feHasta"].ToString());
            this.hdnFeIniProcAnual.Value = dr["feDesde"].ToString();
            this.hdnFeFinProcAnual.Value = dr["feHasta"].ToString();

        }

        public new void ClearEditPopup()
        {
            this.txtfeVacaPp.Text = "";
            this.txtidPlanillaPp.Text = "";
            this.txtmtMontoPp.Text = "";
            this.txtnuDiasVacaPp.Text = "";
            //this.txtPeriVacPp.Text = ""; 

        }

        public override void SaveData(String key)
        {
            if (this.ValidateData() == false)
                return;

            IEntityBase objE = (IEntityBase)this.CreateEntitySet();
            IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.PerVacaDesca));

            String[] strRet = objPro.UpdateData(objE);
            String strMsg = "";

            switch (this.FormEditAction)
            {

                case FormEditAction.AddNew:
                case FormEditAction.Copy:
                    strMsg = "Se ha registrado el Prestamo : " + strRet[0].ToString();
                    break;
                default:
                    strMsg = "Se ha modificado el Prestamo : " + this.txtidVacaDesca.Text;
                    break;
            }
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfGestionDescBnd.aspx");
        }

        public override void DeleteData()
        {
            IEntityBase objE = (IEntityBase)this.CreateEntitySet();
            IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));
            objPro.DeleteData(objE);

            //this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Se anulo el derechohabiente  : " + txtidPersonal.Text, "wfPerDereHabiBnd.aspx");

        }

        public override Object CreateEntitySet()
        {
            EPerVacaDesca objE = new EPerVacaDesca();

            if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
                objE.EntityState = EntityState.Added;
            else
            {
                IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.PerVacaDesca));
                objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
                objE.IdPersonal = Request.QueryString["IdPersonal"].ToString();
                objE.IdVacaDesca = this.txtidVacaDesca.Text;
                objE = (EPerVacaDesca)objBO.GetByKey(objE);

                if (this.FormEditAction == FormEditAction.Edit)
                    objE.EntityState = EntityState.Modified;
                if (this.FormEditAction == FormEditAction.Delete)
                    objE.EntityState = EntityState.Deleted;

            }

            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();

            WebHelper.ControlToEntity(txtidVacaDesca, objE, "IdVacaDesca");
            WebHelper.ControlToEntity(hdnidPlanilla, objE, "IdPlanilla");
            WebHelper.ControlToEntity(hdnidPersonal, objE, "IdPersonal");
            WebHelper.ControlToEntity(ddlidForPago, objE, "IdForPago");
            WebHelper.ControlToEntity(txtdsObservacion, objE, "DsObserva");

            objE.StVaca = ConstEstadoVacaciones.PENDIENTE;
            objE.StAnulado = ConstFlagEstado.DESACTIVADO;

            DataTable dt = (DataTable)this.DataSource;

            this.ClearDeletedRow(this.DataSource, "stAnulado");
            IList<EPerVacaDescaDet> colPerDesca = new List<EPerVacaDescaDet>();

            if (this.FormEditAction == FormEditAction.Delete)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.MarkDeletedRow(dr, "stAnulado");
                }
            }

            for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
            {
                DataTable dtChange = null;
                if (intTypeUpd == 0)
                    dtChange = dt.GetChanges(DataRowState.Added);
                else
                    dtChange = dt.GetChanges(DataRowState.Modified);


                if (dtChange != null)
                {
                    foreach (DataRow drDet in dtChange.Rows)
                    {
                        EPerVacaDescaDet objEDet = DataHelper.CopyDataRowToEntity<EPerVacaDescaDet>(drDet, typeof(EPerVacaDescaDet));

                        if (drDet.RowState == DataRowState.Added)
                            objEDet.EntityState = EntityState.Added;

                        if (drDet.RowState == DataRowState.Modified)
                        {
                            if (objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                                objEDet.EntityState = EntityState.Modified;
                            else
                                objEDet.EntityState = EntityState.Deleted;
                        }

                        colPerDesca.Add(objEDet);
                    }
                }
            }

            EPerVacaDescaSet objESet = new EPerVacaDescaSet();
            objESet.PerVacaDesca = objE;
            objESet.ColVacaDescanso = colPerDesca;

            return objESet;
        }

        public override void LockControls()
        {
            if (this.FormEditAction != FormEditAction.AddNew)
            {
                //this.txtmtprestamo.Enabled = false;
                //this.txtidPersonal.EnabledButton = false;
                //this.txtidPlanilla.EnabledButton = false;
                //this.txtfePrestamo.Enabled = false;
                //this.ddlidMoneda.Enabled = false; 
            }
        }

        public void CalcularCuotas()
        {


        }
    }
}